package com.ys.peony.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ys.peony.R;
import com.ys.peony.app.BaseApplication;
import com.ys.peony.utils.StringUtils;

public class HistorySearchAdapter extends BaseAdapter {

    private String[] list;
    private Context context;

    public HistorySearchAdapter(Context context, String[] list){
        this.list=list;
        this.context=context;
    }

    @Override
    public int getCount() {
        if(list==null||list.length<=0){
            return 0;
        }else{
            return list.length+1;
        }

    }

    @Override
    public String getItem(int position) {
        return list==null? null:list[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHold viewHold=null;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.search_key_item_layout,null);
            viewHold=new ViewHold();
            viewHold.hisKeyTv=(TextView) convertView.findViewById(R.id.name_tv);
            convertView.setTag(viewHold);
        }else{
            viewHold=(ViewHold) convertView.getTag();
        }

        if(list!=null &&list.length>0){
            if(position==list.length){
                viewHold.hisKeyTv.setText("清除历史记录");
                viewHold.hisKeyTv.setTextColor(BaseApplication.getInstance().getResources().getColor(R.color.color_1787E3));
            }else{
                if(!StringUtils.isEmpty(list[position])){
                    viewHold.hisKeyTv.setText(list[position]);
                    viewHold.hisKeyTv.setTextColor(BaseApplication.getInstance().getResources().getColor(R.color.color_7d7d7d));
                }
            }
        }

        return convertView;
    }



    class ViewHold{
        private TextView hisKeyTv;
    }

    public void setDate(String[] hisKeyList) {
        this.list=hisKeyList;
        notifyDataSetChanged();
    }
}
